package spittr.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import spittr.model.User;
import spittr.service.UserService;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/register")
public class RegisterController {
    @Autowired
    UserService service;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String showRegisterPage(){
        return "registerPage";
    }

    @RequestMapping(value = "/regist")
    public String regist(@RequestPart("headPicture") MultipartFile headPicture, User user, HttpServletRequest request){
        String contextPath = request.getServletContext().getInitParameter("fileUploadLocation");
        service.addUser(headPicture,contextPath, user);
        return "redirect:/spitter/"+user.getUsername();
    }

}
